####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Fig A16, A17, Table A8
###
####################################################################################


library(miceadds)
library(dplyr)
library(ggplot2)
library(rlist)

################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))



################################################################################
# Plot Functions
################################################################################

makeplot_split = function(model_output, hete_var){
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  container = lapply(model_output, function(x) rbind(cbind(summary(x)[2:6,], period = c(-4, -3, -2, 1, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=6))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}




################################################################################
# Vote for the extreme left (Figure A16)
################################################################################

modelli = list()
df_did$y = df_did$extreme.left
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$y) & df_did$imm_quant_99==n),]
  temp = lm.cluster(data=df1, y ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
                      factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  modelli[[n]] = temp
}

makeplot_split(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA16.pdf'), height=8, width=12)



################################################################################
# Vote for the right (Figure A17)
################################################################################


modelli = list()
df_did$y = df_did$right
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$y) & df_did$imm_quant_99==n),]
  temp = lm.cluster(data=df1, y ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
                      factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  modelli[[n]] = temp
}

makeplot_split(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA17.pdf'), height=8, width=12)




################################################################################
# Table A8
################################################################################


cleantable = function(container){
  colnames(container)=c('coef', 'se', 'tval', 'pval', 'obs', 'mean', 'model', 'tercile')
  container[,1:6] = sapply(container[,1:6], as.numeric)
  container[,1:6] = round(container[,1:6], 3)
  container$se = paste0('(',  as.character(container$se), ')')
  container$coef = ifelse(container$pval<0.01, paste0(as.character(container$coef), '***'), 
                          ifelse(container$pval>0.01 & container$pval<0.05, paste0(as.character(container$coef), '**'),
                                 ifelse(container$pval>0.05 & container$pval<1, paste0(as.character(container$coef), '*'), as.character(container$coef))))
  container = t(container)
  colnames(container) = container['model',]
  return(container)
}


container = data.frame()
for (n in c(0, 1,2,3)){
  print(paste0('--------------Immigation tercile ', n, ' -------------- '))
  
  df1 = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_share_90) & !is.na(df_did$hlm_share_90)),]
  if (n>0){df1 = df1[which(df1$imm_quant_99==n),]}
  
  print(paste0(' * Extreme Left *'))
  temp = lm.cluster(data=df1, extreme.left ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$extreme.left),]), mean(df1$extreme.left, na.rm = T ), 'extr left', n))
  
  print(paste0(' * Left *'))
  temp = lm.cluster(data=df1, left ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$left),]), mean(df1$left, na.rm = T ), 'left', n))
  
  print(paste0(' * Right *'))
  temp = lm.cluster(data=df1, right ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$right),]), mean(df1$right, na.rm = T ), 'right', n))
  
  print(paste0(' * FN *'))
  temp = lm.cluster(data=df1, fn ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$fn),]), mean(df1$fn, na.rm = T ) , 'fn', n))
  
  print(paste0(' * turnout *'))
  temp = lm.cluster(data=df1, turnout ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$turnout),]), mean(df1$turnout, na.rm = T ), 'turnout', n))
  
}


output = cleantable(container)
write.csv(output, paste0(wd_res, '/tables/tabA8.csv'))
